﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Shell
{
    public partial class SelectTargetWindow : Form
    {
        public SelectTargetWindow()
        {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            APIUtil.EnumWindows(OnWndEnumProc, (uint)0);
        }

        private bool OnWndEnumProc(IntPtr hWnd, uint lParam)
        {
            StringBuilder title = new StringBuilder(255);
            APIUtil.GetWindowText(hWnd, title, title.Capacity);
            if (!string.IsNullOrEmpty(title.ToString()))
            {
                int index = listBox1.Items.Add(new WndInfo(hWnd, string.Format("{0}({1})", title, hWnd)));
            }
            return true;
        }
    }

    public class WndInfo
    {
        public string Caption { get; set; }
        public IntPtr HWND { get; set; }
        public WndInfo(IntPtr hWnd, string caption)
        {
            HWND = hWnd;
            Caption = caption;
        }
        public override string ToString()
        {
            return Caption;
        }
    }
}
